package com.maven.front.config;

import com.maven.front.interceptor.MyInterceptorFront;
import com.maven.front.interceptor.MyInterceptorManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    //将后台拦截器加载spring容器中
    @Bean
    public MyInterceptorManager myInterceptorManager(){
        return new MyInterceptorManager();
    }

    //前台拦截器加入容器
    @Bean
    public MyInterceptorFront myInterceptorFront(){

        return new MyInterceptorFront();
    }
//    拦截器规则
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptorFront())
                .excludePathPatterns("/admin/**") //后台全放行
                .excludePathPatterns("/user/login")//设置不拦截的路径
                .excludePathPatterns("/user/isLogin")  //验证登录
                .excludePathPatterns("/user/register") //注册
                .excludePathPatterns("/user/index")  //首页显示
                .excludePathPatterns("/user/index/allCourse")
                .excludePathPatterns("/user/index/freshCourse")
                .excludePathPatterns("/user/index/recommendCourse")
                .excludePathPatterns("/user/questions/show")
                .excludePathPatterns("/user/questions/showSingle")
                .excludePathPatterns("/user/questions/showSingle")
                .excludePathPatterns("/user/courseVideo/init")
                .excludePathPatterns("/user/subject")
                .excludePathPatterns("/user/subject/sort")
                .excludePathPatterns("/user/teacher/selSubject")
                .excludePathPatterns("/user/teacher/getList")
                .excludePathPatterns("/user/teacher/getById")
                .excludePathPatterns("/user/teacher/selectKey")
                .excludePathPatterns("/user/teacher/getSubjectId")
                .excludePathPatterns("/user/e/listAll")
                .excludePathPatterns("/user/e/get")
                .excludePathPatterns("/user/e/selectAll")
                .excludePathPatterns("/user/e/insert1")
                .addPathPatterns("/user/*");            //拦截路径 ，/**  所有
        registry.addInterceptor(myInterceptorManager())
                .excludePathPatterns("/user/**")  //前台全放行
                .excludePathPatterns("/admin/sysUser/login") //放行后台登录验证
                .excludePathPatterns("/admin/eduCourse/saveImg")
                .excludePathPatterns("/admin/kpoint/setVideo")
                .excludePathPatterns("/admin/sysSubject/selectSysSubject")
                .excludePathPatterns("/admin/sysSubject/getSubject")
                .excludePathPatterns("/admin/sysSubject/selectParentId")
                .excludePathPatterns("/admin/eduCourse/getById")
                .excludePathPatterns("/admin/teacher/upload")
                .excludePathPatterns("/admin/eduUser/list")
                .excludePathPatterns("/admin/edustAll")
                .excludePathPatterns("/admin/edu/getid")
                .excludePathPatterns("/admin/edusave")
                .excludePathPatterns("/admin/edulist")
                .excludePathPatterns("/admin/edulistedu")
                .excludePathPatterns("/admin/edu/getName")
                .excludePathPatterns("/admin/edu/getName1")
                .excludePathPatterns("/admin/addarticle/getfile")
                .excludePathPatterns("/admin/article/delect")
                .addPathPatterns("/admin/**");      //拦截后台其他

    }

//    跨域设置规则
    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");//允许所有的ip访问
        corsConfiguration.addAllowedHeader("*");//允许所有的header参数
        corsConfiguration.addAllowedMethod("*");//允许所有的请求方式
        corsConfiguration.setAllowCredentials(true);//允许携带cookie
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    //跨域
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }
}
